package zozopo.operations.comm;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;


public class DictionaryOS extends OperatingService{
	private static final Log logger = LogFactory.getLog(DictionaryOS.class);
	
	public DictionaryOS(Connection conn){
		super(conn);
		
	}
	
	public DictionaryOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}

	private static Map<String,DictionaryTB> dictionaryLableMap = null;
	private static List<DictionaryTB> dictionaryLableList = null;
	private static Map<String,DictionaryTB> dictionaryLimitMap = null;
	private static Map<String,DictionaryTB> dictionaryCodeMap = null;
	private static Map<String,DictionaryTB> dictionaryKindMap = null;
	//private static dictionaryMap
	
	
	public Map<String, DictionaryTB> getDictionaryMap() {
		if(dictionaryCodeMap == null)initDictionaryMap();
		return dictionaryCodeMap;
		
	}

	public void setDictionaryMap(Map<String, DictionaryTB> dictionaryMap) {
		DictionaryOS.dictionaryCodeMap = dictionaryMap;
	}

	public  Map<String, DictionaryTB> getDictionaryLimitMap() {
		if(dictionaryLimitMap == null)initLimitDictionary();
		return dictionaryLimitMap;
	}



	public Map<String, DictionaryTB> getDictionaryLableMap() {
		if(dictionaryLableMap == null)initLableDictionary();
		return dictionaryLableMap;
	}
	
	public List<DictionaryTB> getDictionaryLableList() {
		if(dictionaryLableList == null){
			initLableDictionary();
		}
		return dictionaryLableList;
	}
	

	
	public String getDictValue(String kind){
		
		if(kind == null || kind.trim().equals("")){
			return "";
		}
		
		if(dictionaryCodeMap == null)initDictionaryMap();
		DictionaryTB dictionary = dictionaryCodeMap.get(kind);
		
		if(dictionary == null){
			//dictionary.setDictionary_kind(kind);
			dictionary.setDictionary_value("数据不在数据库在存在");
		}
	
		return dictionary.getDictionary_value();
	}

	
	
	private void initLableDictionary() {
		dictionaryLableMap = new LinkedHashMap<String,DictionaryTB>();
		dictionaryLableList = new ArrayList<DictionaryTB>();
		dictionaryKindMap  = new LinkedHashMap<String,DictionaryTB>();
		SqlMachine sqlMachine = new SqlMachine(conn);
		ResultSet result = sqlMachine.QueryRow(DictionaryTB.TABLE_NAME,DictionaryTB.DICTIONARY_KIND,"MANUAL.LABLE");
		                                                                                             
		try {
			while(result.next()){
				 DictionaryTB dictionary = new DictionaryTB();
				 dictionary.setDictionary_id(result.getString(DictionaryTB.DICTIONARY_ID));
				 dictionary.setDictionary_code(result.getString(DictionaryTB.DICTIONARY_CODE));
				 dictionary.setDictionary_value(result.getString(DictionaryTB.DICTIONARY_VALUE));
				 dictionary.setDictionary_kind(result.getString(DictionaryTB.DICTIONARY_KIND));
				 dictionary.setDictionary_remark(result.getString(DictionaryTB.DICTIONARY_REMARK));
				 
				 dictionary.setDictionary_enname(result.getString(DictionaryTB.DICTIONARY_ENNAME));
				 dictionary.setDictionary_cnname(result.getString(DictionaryTB.DICTIONARY_CNNAME));
				 
				 dictionary.setDictionary_source_key(result.getString(DictionaryTB.DICTIONARY_SOURCE_KEY));
				 dictionaryLableMap.put(result.getString(DictionaryTB.DICTIONARY_CODE), dictionary);
				 dictionaryKindMap.put(result.getString(DictionaryTB.DICTIONARY_KIND), dictionary);
				 dictionaryLableList.add(dictionary);
			}
			result.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	private void initDictionaryMap() {
		dictionaryCodeMap = new LinkedHashMap<String,DictionaryTB>();
		SqlMachine sqlMachine = new SqlMachine(conn);
	
		ResultSet result = sqlMachine.selectAllRecord(DictionaryTB.TABLE_NAME);
		                                                                                             
		try {
			while(result.next()){
				 DictionaryTB dictionary = new DictionaryTB();
				 dictionary.setDictionary_id(result.getString(DictionaryTB.DICTIONARY_ID));
				 dictionary.setDictionary_code(result.getString(DictionaryTB.DICTIONARY_CODE));
				 dictionary.setDictionary_value(result.getString(DictionaryTB.DICTIONARY_VALUE));
				 dictionary.setDictionary_kind(result.getString(DictionaryTB.DICTIONARY_KIND));
				 
				 dictionary.setDictionary_enname(result.getString(DictionaryTB.DICTIONARY_ENNAME));
				 dictionary.setDictionary_cnname(result.getString(DictionaryTB.DICTIONARY_CNNAME));
				 
				 dictionary.setDictionary_remark(result.getString(DictionaryTB.DICTIONARY_REMARK));
				 dictionary.setDictionary_source_key(result.getString(DictionaryTB.DICTIONARY_SOURCE_KEY));
				 dictionaryCodeMap.put(result.getString(DictionaryTB.DICTIONARY_CODE), dictionary);
			}
			result.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void initLimitDictionary() {
		dictionaryLimitMap = new LinkedHashMap<String,DictionaryTB>();
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		ResultSet result = sqlMachine.QueryRow(DictionaryTB.TABLE_NAME,DictionaryTB.DICTIONARY_KIND,"MANUAL.USER.LIMITFLAG");
		                                                                                             
		try {
			while(result.next()){
				 DictionaryTB dictionary = new DictionaryTB();
				 dictionary.setDictionary_id(result.getString(DictionaryTB.DICTIONARY_ID));
				 dictionary.setDictionary_code(result.getString(DictionaryTB.DICTIONARY_CODE));
				 dictionary.setDictionary_value(result.getString(DictionaryTB.DICTIONARY_VALUE));
				 dictionary.setDictionary_kind(result.getString(DictionaryTB.DICTIONARY_KIND));
				 dictionary.setDictionary_enname(result.getString(DictionaryTB.DICTIONARY_ENNAME));
				 dictionary.setDictionary_cnname(result.getString(DictionaryTB.DICTIONARY_CNNAME));
				 
				 dictionary.setDictionary_remark(result.getString(DictionaryTB.DICTIONARY_REMARK));
				 dictionary.setDictionary_source_key(result.getString(DictionaryTB.DICTIONARY_SOURCE_KEY));
				 dictionaryLimitMap.put(result.getString(DictionaryTB.DICTIONARY_CODE), dictionary);
			}
			result.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public Map<String, DictionaryTB> getDictionaryCodeMapByKind(String kind){
		Map<String,DictionaryTB> dictionaryMap = new LinkedHashMap<String,DictionaryTB>();
		SqlMachine sqlMachine = new SqlMachine(conn);
		ResultSet result = sqlMachine.QueryRow(DictionaryTB.TABLE_NAME,DictionaryTB.DICTIONARY_KIND,kind);
		                                                                                             
		try {
			while(result.next()){
				 DictionaryTB dictionary = new DictionaryTB();
				 dictionary.setDictionary_id(result.getString(DictionaryTB.DICTIONARY_ID));
				 dictionary.setDictionary_code(result.getString(DictionaryTB.DICTIONARY_CODE));
				 dictionary.setDictionary_value(result.getString(DictionaryTB.DICTIONARY_VALUE));
				 dictionary.setDictionary_kind(result.getString(DictionaryTB.DICTIONARY_KIND));
				 dictionary.setDictionary_enname(result.getString(DictionaryTB.DICTIONARY_ENNAME));
				 dictionary.setDictionary_cnname(result.getString(DictionaryTB.DICTIONARY_CNNAME));
				 dictionary.setDictionary_remark(result.getString(DictionaryTB.DICTIONARY_REMARK));
				 dictionary.setDictionary_source_key(result.getString(DictionaryTB.DICTIONARY_SOURCE_KEY));
				 dictionaryMap.put(dictionary.getDictionary_code(), dictionary);
			}
			result.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return dictionaryMap;
	}
	
	public Map<String, DictionaryTB> getDictionaryValueMapByKind(String kind){
		Map<String,DictionaryTB> dictionaryMap = new LinkedHashMap<String,DictionaryTB>();
		SqlMachine sqlMachine = new SqlMachine(conn);
		ResultSet result = sqlMachine.QueryRow(DictionaryTB.TABLE_NAME,DictionaryTB.DICTIONARY_KIND,kind);
		                                                                                             
		try {
			while(result.next()){
				 DictionaryTB dictionary = new DictionaryTB();
				 dictionary.setDictionary_id(result.getString(DictionaryTB.DICTIONARY_ID));
				 dictionary.setDictionary_code(result.getString(DictionaryTB.DICTIONARY_CODE));
				 dictionary.setDictionary_value(result.getString(DictionaryTB.DICTIONARY_VALUE));
				 dictionary.setDictionary_kind(result.getString(DictionaryTB.DICTIONARY_KIND));
				 dictionary.setDictionary_enname(result.getString(DictionaryTB.DICTIONARY_ENNAME));
				 dictionary.setDictionary_cnname(result.getString(DictionaryTB.DICTIONARY_CNNAME));
				 dictionary.setDictionary_remark(result.getString(DictionaryTB.DICTIONARY_REMARK));
				 dictionary.setDictionary_source_key(result.getString(DictionaryTB.DICTIONARY_SOURCE_KEY));
				 dictionaryMap.put(dictionary.getDictionary_value(), dictionary);
			}
			result.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return dictionaryMap;
	}
	
	public DictionaryTB getENNameByKindAndValue(String kind,String value){
		
		Map<String,DictionaryTB> dictionaryMap = getDictionaryValueMapByKind(kind);
		DictionaryTB dictionary = (DictionaryTB)dictionaryMap.get(value);
		return dictionary;
	}
}
